package futbol.tacticas.concursantes.The1A;

import java.awt.Point;

public class FutureSimulator {
	private BallBehaviour bh;
	private int[] playersSpeed;
	private Point[] playersPosition;
	
	public FutureSimulator(int[] playersSpeed){
		this.playersSpeed = playersSpeed;
		playersPosition = null;
	}	
	
	public FutureSimulator(BallBehaviour bh, int[] playersSpeed){
		this.bh = bh;
		this.playersSpeed = playersSpeed;
		playersPosition = null;
	}
	
	public void setPlayersPosition(Point[] playersPosition){
		this.playersPosition = playersPosition;
	}
	

	public FutureNearest getFutureNearestPlayer(){
		Point[] futureBall = bh.getPredictions();	
		double minDist = 1000;
		int playerMin = 0;
		
		for (int i = bh.getCatchable(); i<futureBall.length; i++){
			Point ball = futureBall[i];
			for (int k = playersPosition.length - 1; k >= 0; k--) {
				Point p = playersPosition[k];
				double dist = Utils.Distance(ball, p);
				if (dist < (11 + playersSpeed[k] * i) && dist < minDist)
				{
					minDist = dist;
					playerMin = k;
				}
			}
			if (minDist < 1000)
				return new FutureNearest(playerMin,i,ball.getX(), ball.getY()); 
		}		
		
		return null;
	}
	
	public FutureNearest getFutureNearestPlayer(Point[] futureBall, int catchable){
					
		int deb = Math.max(catchable, 1);
		
		if (Utils.isGoodGoal(futureBall[deb])) // COOL !
			return new FutureNearest(-1,0,160,0);
		else if (futureBall[deb].x == -160) // Not Cool :(
			return new FutureNearest(100,0,-160,0);
			
		
		for (int i = deb; i<futureBall.length; i++){
			Point ball = futureBall[i];
			
			for (int k = playersPosition.length - 1; k >= 0; k--) {
				double dist = Utils.Distance(ball, playersPosition[k]);
				int distShoot = 9;
				if (k>10)
					distShoot = 11;
				if (dist < (distShoot + playersSpeed[k] * i))
					return new FutureNearest(k,i,futureBall[i].getX(),futureBall[i].getY());
			}
		}	
		
		return new FutureNearest(0,futureBall.length,0,0);
	}	
}
